package com.abcd.godating.web;

import java.security.Principal;
import java.util.List;

import javax.inject.Inject;

import org.bson.types.ObjectId;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abcd.godating.domain.Message;
import com.abcd.godating.service.MessageService;

@RequestMapping("/message/*")
public class MessageController {

	private final int limit = 30;

	@Inject
	private MessageService messageService;

	@RequestMapping(value = "/deleteIncoming/{ids}")
	public @ResponseBody
	void deleteIncoming(Principal principal, @PathVariable ObjectId... ids) {
		long userId = Long.valueOf(principal.getName());
		messageService.delete(userId, false, ids);
	}

	@RequestMapping(value = "/deleteOutGoing/{ids}")
	public @ResponseBody
	void deleteOutGoing(Principal principal, @PathVariable ObjectId... ids) {
		long userId = Long.valueOf(principal.getName());
		messageService.delete(userId, true, ids);
	}

	@RequestMapping(value = "/between/{withWho}/{skip}")
	public @ResponseBody
	List<Message> getBetween(Principal principal, @PathVariable long withWho,
			@PathVariable int skip) {
		long userId = Long.valueOf(principal.getName());
		return messageService.getBetween(userId, withWho, skip, limit);
	}

	@RequestMapping(value = "/imcoming/{skip}")
	public @ResponseBody
	List<Message> getIncoming(Principal principal, @PathVariable int skip) {
		long userId = Long.valueOf(principal.getName());
		return messageService.getIncoming(userId, skip, limit);
	}

	@RequestMapping(value = "/imcomingUnRead/{skip}")
	public @ResponseBody
	List<Message> getIncomingUnRead(Principal principal, @PathVariable int skip) {
		long userId = Long.valueOf(principal.getName());
		return messageService.getIncomingUnRead(userId, skip, limit);
	}

	@RequestMapping(value = "/outGoing/{skip}")
	public @ResponseBody
	List<Message> getOutgoing(Principal principal, @PathVariable int skip) {
		long userId = Long.valueOf(principal.getName());
		return messageService.getOutgoing(userId, skip, limit);
	}

	@RequestMapping(value = "/hasUnread")
	public @ResponseBody
	boolean hasUnread(Principal principal) {
		long userId = Long.valueOf(principal.getName());
		return messageService.hasUnread(userId);
	}

	@RequestMapping(value = "/readIncoming/{id}")
	public @ResponseBody
	Message readIncoming(Principal principal, @PathVariable ObjectId id) {
		long userId = Long.valueOf(principal.getName());
		return messageService.read(id, userId, false);
	}

	@RequestMapping(value = "/readOutGoing/{id}")
	public @ResponseBody
	Message readOutGoing(Principal principal, @PathVariable ObjectId id) {
		long userId = Long.valueOf(principal.getName());
		return messageService.read(id, userId, true);
	}

	@RequestMapping(value = "/sent/{id}/{content}")
	public @ResponseBody
	Message sent(Principal principal, @PathVariable long to,
			@PathVariable String content) {
		long userId = Long.valueOf(principal.getName());
		return messageService.sent(userId, to, content);
	}
}
